package com.bc.mes.api.controller.dv;

import com.bc.mes.common.api.CommonResult;
import com.bc.mes.common.dto.dv.DvMachineryTypeParam;
import com.bc.mes.common.util.JwtTokenUtil;
import com.bc.mes.device.service.DvMachineryTypeService;
import com.bc.mes.mbg.model.dv.DvMachineryType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * --- 天道酬勤 ---
 *
 * @author zhangpingan
 * @date 2024/7/11
 * @desc
 */
@RequestMapping("/dv")
@Controller
@CrossOrigin(allowCredentials = "true")
@Api(tags = "DvMachineryType",description = "设备类型")
public class DvMachineryTypeController {

    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private DvMachineryTypeService dvMachineryTypeService;

    @ApiOperation(value = "列出设备的所有类型")
    @ResponseBody
    @RequestMapping(value = "machinerytype/list",method = RequestMethod.GET)
    public CommonResult<List<DvMachineryType>> list(String machineryTypeName,String enableFlag){
       return dvMachineryTypeService.list(machineryTypeName,enableFlag);
    }

    @ResponseBody
    @RequestMapping(value="machinerytype/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "根据id查询设备类型")
    public CommonResult<DvMachineryType> selectById(@PathVariable("id") Long id){
        return dvMachineryTypeService.selectById(id);
    }

    @ApiOperation(value = "更新设备类型")
    @ResponseBody
    @RequestMapping(value="machinerytype",method = RequestMethod.PUT)
    public CommonResult update(@RequestBody DvMachineryTypeParam dvMachineryTypeParam,HttpServletRequest request){
        String username = getUsername(request);
        dvMachineryTypeParam.setCreateBy(username);
        dvMachineryTypeParam.setUpdateTime(new Date());

        int i = dvMachineryTypeService.update(dvMachineryTypeParam);

        if(i>0){
            return CommonResult.success(200,"更新成功");
        }
        return CommonResult.failed();
    }


    @ApiOperation(value = "新增设备类型")
    @ResponseBody
    @RequestMapping(value="machinerytype",method = RequestMethod.POST)
    public CommonResult insert(@RequestBody  DvMachineryTypeParam dvMachineryTypeParam , HttpServletRequest request){
        String username = getUsername(request);
        dvMachineryTypeParam.setCreateBy(username);
        dvMachineryTypeParam.setCreateTime(new Date());

        int i = dvMachineryTypeService.insert(dvMachineryTypeParam);
        if(i>0){
            return CommonResult.success(200,"新增成功");
        }
        return CommonResult.failed();
    }
    @ApiOperation(value = "删除设备类型")
    @ResponseBody
    @RequestMapping(value="machinerytype/{id}",method = RequestMethod.DELETE)
    public CommonResult delete(@PathVariable("id") Long id){
       CommonResult result =  dvMachineryTypeService.delete(id);

        return result;
    }






    private String getUsername(HttpServletRequest request){
        String token = request.getHeader(this.tokenHeader);
        if (token != null && token.startsWith(this.tokenHead)) {

            String authToken = token.substring(this.tokenHead.length());// The part after "Bearer "

            String username = jwtTokenUtil.getUserNameFromToken(authToken);
            return username;
        }
        return null;
    }
}
